# -*- coding: utf-8 -*-
__author__ = 'Yh'

from pymongo.mongo_client import MongoClient
from BBSSpider.settings import MONGO_URI, MONGO_DATABASE, MONGO_COLLECTION

client = MongoClient()

class LectureUtils(object):

    collections_name = MONGO_COLLECTION['lecture']

    def __init__(self):
        self.mongo_uri = MONGO_URI
        self.mongo_db = MONGO_DATABASE
        self.client = MongoClient(self.mongo_uri)
        self.db = self.client[self.mongo_db]

    @staticmethod
    def get_lecture_max_sequence(self):
        pipeline = [
            {'$sort': {'sequence': -1}},
            {'$limit': 1}
        ]
        rl = list(self.db[self.collections_name].aggregate(pipeline))

        result = 0
        if list and len(rl) > 0:
            result = rl[0]['sequence']

        client.close()

        return result


class ForumUtils(object):

    collections_name = MONGO_COLLECTION['article']

    def __init__(self):
        self.mongo_uri = MONGO_URI
        self.mongo_db = MONGO_DATABASE
        self.client = MongoClient(self.mongo_uri)
        self.db = self.client[self.mongo_db]

    # 根据讨论区名称获得文章数，若文章数没有变化，则不更新
    @staticmethod
    def get_popularity_by_sub_category(self, category):
        return 0

    def update_replay_sequence(self, parent_sequence, replay_sequence):
        self.db[self.collections_name].update_one({'sequence': parent_sequence}, {'$push': {'replys': replay_sequence}})
        self.client.close()
